#include <stdio.h>
#include <string.h>
#include "wifi_ap.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "wifi_device.h"
#include "wifi_hotspot_config.h"
#include "utils_log.h"

#define MAX_WIFI_NAME_LEN   32
#define MIN_DEVICE_SN_LEN   6
#define MAX_DEVICE_SN_LEN   19
#define DEFAULT_WIFI_AP_PSW  "12345678"
#define DEFAULT_DEVICE_SN  "0151IC204P23880001"

static WifiEvent g_wifiEventHandler = {0};
static WifiEventState g_hotspotState = WIFI_STATE_NOT_AVAILABLE;
static void OnHotspotStaJoinHandler(StationInfo *info)
{
    (void)info;
    printf("STA join AP\n");
    return;
}

static void OnHotspotStaLeaveHandler(StationInfo *info)
{
    if (NULL == info){
        printf("HotspotStaLeave:info is null\n");
        LOG_TO_FILE("HotspotStaLeave:info is null");
        return;
    }
    
    printf("HotspotStaLeave:disconnect reason is %d.\n", info->disconnectedReason);
    return;
}

static void OnHotspotStateChangedHandler(int state)
{
    g_hotspotState = state;
    printf("HotspotStateChanged:Wifi hotspot state is %s.\n", state ? "Avilable":"Unavilable");
    return;
}

int StartWifiAP(void)
{
    printf("<--Wifi Init-->\r\n");
    g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
    WifiErrorCode err = RegisterWifiEvent(&g_wifiEventHandler);
    if (err != WIFI_SUCCESS) {
        printf("register wifi event fail!\r\n");
    } else {
        printf("register wifi event succeed!\r\n");
    }

    char snStr[MAX_DEVICE_SN_LEN] = {0};
    char *tmpSN = getDeviceSN();        //获取device SN
    if(NULL == tmpSN){ 
        setDeviceSN(DEFAULT_DEVICE_SN);
        memcpy(snStr, DEFAULT_DEVICE_SN, MAX_DEVICE_SN_LEN); //设置默认SN 
    } else {
        memcpy(snStr, tmpSN, strlen(tmpSN));            
    }

    char *tmp;
    int len = strlen(snStr);
    if(len < MIN_DEVICE_SN_LEN) {
        tmp = "001";
    } else {
        tmp = &snStr[len-3];
        tmp[MIN_DEVICE_SN_LEN-1] = '\0';
    }
 
    char ssid[MAX_WIFI_NAME_LEN] = {0};
    snprintf(ssid, MAX_WIFI_NAME_LEN, "kaihong%s", tmp);   //拼接WiFi名称
    printf("Wifi AP ssid is %s\n", ssid);

    HotspotConfig apConfig = {
        .preSharedKey = DEFAULT_WIFI_AP_PSW,
        .securityType = WIFI_SEC_TYPE_PSK,
    };
    memcpy(apConfig.ssid, ssid, strlen(ssid));

    err = SetHotspotConfig(&apConfig);
    if (err != WIFI_SUCCESS) {
        printf("SetHotspotConfig failed, wifi_error = %d\n", err);
        LOG_TO_FILE("SetHotspotConfig failed, wifi_error = %d", err);
        return -1;
    }

    err = EnableHotspot();
    if (err != WIFI_SUCCESS) {
        printf("EnableHotspot failed, wifi_error = %d\n", err);
        LOG_TO_FILE("EnableHotspot failed, wifi_error = %d", err);
        return -1;
    }

    return 0;
}

bool IsHotspotStateAvailable()
{
    if(g_hotspotState == WIFI_STATE_NOT_AVAILABLE){
        return false;
    } else {
        return true;
    }
}

void  StopWifiAP()
{
    DisableHotspot();
    UnRegisterWifiEvent(&g_wifiEventHandler);
    g_hotspotState = WIFI_STATE_NOT_AVAILABLE;
}
